//
//  DDWelcomeVC.swift
//  SinaMicroBlogSwift2
//
//  Created by dqw on 15/7/31.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import SDWebImage

class DDWelcomeVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        loadAndLayoutSubviews()

 // FIXME: - 如果有头像的数据，加载数据，SDWebimage会自动缓存。
        if let avatar = DDUserAccount.aUserAccount?.avatar_large {
            icon.sd_setImageWithURL(NSURL(string: avatar))
        }
    }
    
    var iconBottomConstrain: NSLayoutConstraint?

    // MARK: -  头像动画的执行放在viewdidappear里面，在动画之前做出改变，动画里面调用layoutifneed。结束后，发送通知。
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconBottomConstrain?.constant = -UIScreen.mainScreen().bounds.height - iconBottomConstrain!.constant
        
        UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                NSNotificationCenter.defaultCenter().postNotificationName(DDSwithVCNotificaitonName, object: true)
        }
    }
    
    // MARK: - 加载和布局子控件。
    private func loadAndLayoutSubviews() {
        view.addSubview(backImg)
        view.addSubview(icon)
        view.addSubview(label)
        
        
        backImg.ff_Fill(view)
        
//        backImg.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview" : backImg]))
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview" : backImg]))
        
        let cons = icon.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 90, height: 90), offset: CGPoint(x: 0, y: -200))
        iconBottomConstrain = icon.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        
//        icon.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraint(NSLayoutConstraint(item: icon, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: icon, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 200))
//        iconBottomConstrain = view.constraints.last
        
        
//        view.addConstraint(NSLayoutConstraint(item: icon, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
//         view.addConstraint(NSLayoutConstraint(item: icon, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        
        label.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: icon, size: nil, offset: CGPoint(x: 0, y: 16))
        
//        label.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: icon, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: icon, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        
        
    }
    
    lazy var backImg:UIImageView = UIImageView(image:UIImage(named: "ad_background"))
    lazy var icon:UIImageView = {
        let iconT = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconT.layer.masksToBounds = true
        iconT.layer.cornerRadius = 45
        return iconT
    }()
    lazy var label: UILabel = {
       let labelT = UILabel()
        labelT.text = "欢迎归来"
        labelT.sizeToFit()
        labelT.textColor = UIColor.darkGrayColor()
        return labelT
    }()


}
